<?php
namespace Admin\Controller;


class InformationController extends AdminController {

    public function index(){


        $newsModel = M('Information');

        $map = array();
        $list = $this->lists($newsModel,$map,'sort DESC,id DESC');
        $this->assign('_list', $list);
//        echo "<pre>".print_r($list,true)."</pre>";
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $title = I("title");
            $abstract = I("abstract");
            $content = I("content");

            if(!$title || !$abstract || !$content){
                $this->error('所有输入不能为空');
            }

            $id = I("id",0);

            $model = M("Information");

            $data['title'] = $title;
            $data['abstract'] = $abstract;
            $data["content"] = $content;
            $data['create_time'] = time();

            if($id){
                $re = $model->where("id=".$id)->save($data);
                $msg = "编辑";
            }else{
                $re = $model->add($data);
                $msg = "新增";
            }

            if($re){
                $this->success($msg."科普知识成功",U("Information/index"));
            }else{
                $this->error($msg."科普知识失败");
            }
        }else{

            $id = I("id");
            if($id){
                $model = M("Information");
                $data = $model->where(array('id'=>$id))->find();
                $this->assign("data",$data);
            }
            $this->display();
        }
    }

    public function del(){
        $id = (int)I('id');

        if(!$id){
            echo json_encode(array('errno'=>0));
            die;
        }
        $model = M('Information');
        $re = $model->where("id=".$id)->delete();
        if($re){
            echo json_encode(array('errno'=>1));
            die;
        }else{
            echo json_encode(array('errno'=>0));
            die;
        }
    }
}